const { override, overrideDevServer, } = require("customize-cra");
const DefinePlugin = require("webpack/lib/DefinePlugin");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const SourceMapDevToolPlugin = require("webpack").SourceMapDevToolPlugin;

const path = require("path");
/**
 * 
 * @param {string[]} args 
 * @param {string} key 
 */
const getCommand = (args, key) => {
    const filterCmds = args.filter((cmd) => {
        return cmd.startsWith(key + "=") || cmd.startsWith("--" + key + "=") || cmd.startsWith("-" + key + "=");
    });
    return filterCmds[0] ? filterCmds[0].replace(/^-*[a-z\d_]+=/i,"") : null;
};
const APP_ENV = getCommand(process.argv, "APP_ENV");

module.exports = {
    webpack: override((config) => {
        // config.optimization.minimize
        config.module.rules[1].oneOf[3].include = [
            path.resolve(__dirname, "./src"),
            path.resolve(__dirname, "./packages/ui-builder/src"),
            path.resolve(__dirname, "./packages/ui-antd/src")
        ];
        // throw new Error("Hello");
        return {
            ...config,
            devtool: "cheap-module-source-map",
            watchOptions: {
                ignored: /node_modules/
            },
            module: {
                ...config.module,
                rules: [
                    {
                        test: /\.d\.ts$/,
                        loader: "url-loader"
                    },
                    ...config.module.rules,
                    // {
                    //     test: /\.js\.map$/,
                    //     loader: "file-loader",
                    //     exclude: [
                    //         path.resolve(__dirname, "./node_modules/@microsoft/fetch-event-source")
                    //     ]
                    // }
                ]
            },
            resolve: {
                ...config.resolve,
                extensions: [...config.resolve.extensions, ".js.map"],
                alias: {
                    ...(config.resolve?.alias || {}),
                    "@": path.resolve(process.cwd(), "./src"),
                    "@/*": path.resolve(process.cwd(), "./src/"),
                    "@components": path.resolve(process.cwd(),"./src/components"),
                    "@components/*": path.resolve(process.cwd(),"./src/components/*")
                }
            },
            plugins: [
                ...config.plugins,
                new DefinePlugin({
                    APP_ENV: JSON.stringify(APP_ENV),
                }),
                // new HtmlWebpackPlugin({
                //     inject: true,
                //     "publicPath": "./",
                //     "filename": "./public/index.html",
                //     "templateParameters": {
                //         title: "Admin",
                //         PUBLIC_URL: "./"
                //     }
                // }),
                new SourceMapDevToolPlugin({
                    filename: '[file].map',
                    exclude: [/node_modules\/@microsoft\/fetch-event-source/]
                })
            ],
            // output: {
            //     ...config.output,
            //     publicPath: "./"
            // },
        }
    }),
    // devServer: overrideDevServer((config) => {
    //     console.log(config);
    //     return {
    //         ...config.devServer,
    //         historyApiFallback: true,
    //         devMiddleware: {
    //             publicPath: "/",
    //         },
    //         proxy: {
    //             "/ck_vue/*": {
    //                 target: "http://localhost:3003/",
    //                 changeOrigin: true,
    //                 logLevel: "info",
    //             }
    //         }
    //     }
    // })
};